package com.vnp.cooking;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.w3c.dom.Text;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.viewpagerindicator.CirclePageIndicator;
import com.vnp.cooking.fragment.ImageFragmentLogin;
import com.vnp.cooking.utils.Utils;

import android.os.Bundle;
import android.os.Handler;
import android.os.IInterface;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Shader.TileMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LoginActivity extends FragmentActivity implements OnClickListener{
	
	Button btnLoginIDVatgia;
	Button btnLoginFacebook;
	Button btnDangki;
	MyAdapter mAdapter;
	static ArrayList<Integer> imageUrl = new ArrayList<Integer>();
	ViewPager mPager;
	CirclePageIndicator indicator;
	TextView titleDangnhap, txtChuaTk;
	Handler handler = new Handler();
	Timer swipeTimer = new Timer();
	
	int currentPage = 0;
	int NUM_PAGES = 3;
	private static final String SCREEN_LABEL = "Dang nhap screen";
	
	Runnable update = new Runnable() {
		
		@Override
		public void run() {
			if(NUM_PAGES == currentPage){
				currentPage = 0;
			}
			
			 mPager.setCurrentItem(currentPage++);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		btnLoginIDVatgia = (Button) findViewById(R.id.btn_login_vatgia);
		btnLoginFacebook = (Button) findViewById(R.id.btn_login_fb);
		titleDangnhap = (TextView) findViewById(R.id.title_login);
		txtChuaTk = (TextView) findViewById(R.id.txt_no_account);
		btnDangki = (Button) findViewById(R.id.btn_resgiter);
		
		mAdapter = new MyAdapter(getSupportFragmentManager());
		mPager = (ViewPager) findViewById(R.id.viewpager);
		indicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mPager.setAdapter(mAdapter);
		
		
		indicator.setViewPager(mPager);
		
		 swipeTimer.schedule(new TimerTask() {

		        @Override
		        public void run() {
		            handler.post(update);
		        }
		    }, 100, 2000);
		
		
		init();
		initFont();
		ApplicationContext.getGaTracker().set(Fields.SCREEN_NAME, SCREEN_LABEL);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		ApplicationContext.getGaTracker().send(MapBuilder.createAppView().build());
	}

	@Override
	public void onStop() {
		super.onStop();
	}
	

    
    

   
	
	
	void initFont(){
		btnLoginIDVatgia.setTypeface(Utils.getTypefaceRegular(this));
		btnLoginFacebook.setTypeface(Utils.getTypefaceRegular(this));
		txtChuaTk.setTypeface(Utils.getTypefaceRegular(this));
//		btnDangki.setTypeface(Utils.getTypefaceBold(this));
	}
	
	void init() {
		btnLoginIDVatgia.setOnClickListener(this);
		btnLoginFacebook.setOnClickListener(this);
		btnDangki.setOnClickListener(this);
		createImage();
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login_vatgia:
			gotoLoginIdVatGia();
			break;
			
		case R.id.btn_login_fb:
			gotoLoginFacebook();
			break;
		case R.id.btn_resgiter:
			gotoRegister();
			break;
		default:
			break;
		}
	}
	
	void gotoRegister(){
		Intent t = new Intent(LoginActivity.this, RegisterActivity.class);
		startActivity(t);
		overridePendingTransition(R.anim.open_next, R.anim.close_main);
	}
	
	
	void gotoLoginIdVatGia() {
		Intent t = new Intent(LoginActivity.this, LoginIDVatgiaActivity.class);
		finish();
		startActivity(t);
		overridePendingTransition(R.anim.open_next, R.anim.close_main);
	}
	
	void gotoLoginFacebook() {
		Intent t = new Intent(LoginActivity.this, LoginWithFacebookActivity.class);
		finish();
		startActivity(t);
	}
	
	
	public static class MyAdapter extends FragmentPagerAdapter {
		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return new ImageFragmentLogin(imageUrl.get(0));
			case 1:
				return new ImageFragmentLogin(imageUrl.get(1));
			case 2:
				return new ImageFragmentLogin(imageUrl.get(2));

			default:
				return null;
			}
		}
	}
	
	
	ArrayList<Integer> createImage() {
		
		imageUrl.add(R.drawable.login1_03);
		imageUrl.add(R.drawable.login2_03);
		imageUrl.add(R.drawable.login3_03);
		
		return imageUrl;
	}
	
	
	void randomURl() {
		
	}
	

}
